/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controler;

import Model.Dao.DAODanhMucSanPham;
import Model.Pojo.Danhmucsanpham;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author THANH QUAN
 */
public class DeleteUpdateDanhMuc
        extends HttpServlet
{

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception
    {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setAttribute("QUAN_LY_DANH_MUC", 1);

        String url = request.getServletPath();
        if (url.equals("/delete-danh-muc.htm"))
        {
            String query = request.getQueryString();
            if (query != null)
            {
                String idStr = query.split("=")[1];
                if (idStr != null)
                {
                    int idDanhMuc = Integer.parseInt(idStr);
                    DAODanhMucSanPham DAOdmsp = new DAODanhMucSanPham();
                    DAOdmsp.DeleteDanhMuc(idDanhMuc);
                }
            }
            response.sendRedirect("danh-sach-danh-muc.htm");
            return;
            //view.forward(request, response);
        }
        else
        {
            Danhmucsanpham dmsp = null;
            DAODanhMucSanPham DAOdmsp = null;
            String Redirecturl = "";
            String updateDanhMuc = request.getParameter("btnCapNhat");
            if (updateDanhMuc != null)
            {
                DAOdmsp = new DAODanhMucSanPham();
                dmsp = new Danhmucsanpham();
                dmsp.setId(Integer.parseInt(request.getParameter("hdfIDDM")));
                dmsp.setSoluongsp(Integer.parseInt(request.getParameter("hdfSLSPDM")));
                dmsp.setTendanhmuc(request.getParameter("txtTenDanhMuc").trim());
                dmsp.setKyhieu(request.getParameter("txtKyHieu").trim());
                dmsp.setTinhtrang(Boolean.TRUE);
                if (!DAOdmsp.KiemTraDanhMucTonTai(dmsp))
                {
                    DAOdmsp.UpdateDanhMuc(dmsp);
                    Redirecturl = "danh-sach-danh-muc.htm";
                    response.sendRedirect(Redirecturl);
                    return;
                }
                else
                {
                    request.setAttribute("updateDanhMuc", dmsp);
                    Redirecturl = "them-danh-muc.htm";
                    request.setAttribute("Message", "Lỗi: Danh mục đã tồn tại.");
                }
            }
            else
            {
                String query = request.getQueryString();
                if (query != null)
                {
                    String idStr = query.split("=")[1];
                    if (idStr != null)
                    {
                        int idDanhMuc = Integer.parseInt(idStr);
                        DAOdmsp = new DAODanhMucSanPham();
                        dmsp = DAOdmsp.GetDanhmucsanpham(idDanhMuc);
                        if (dmsp != null)
                        {
                            request.setAttribute("updateDanhMuc", dmsp);
                        }
                        else
                        {
                            request.setAttribute("Message",
                                                 "Lỗi: Không tìm thấy danh mục cần cập nhật.");
                        }
                        Redirecturl = "them-danh-muc.jsp";
                    }
                }
            }
            RequestDispatcher view = request.getRequestDispatcher(Redirecturl);
            view.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        if (!CXuLy.PhanQuyen(request, response))
        {
            return;
        }

        try
        {
            processRequest(request, response);
        }
        catch (Exception ex)
        {
            Logger.getLogger(DeleteUpdateDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        try
        {
            processRequest(request, response);
        }
        catch (Exception ex)
        {
            Logger.getLogger(DeleteUpdateDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo()
    {
        return "Short description";
    }// </editor-fold>
}
